<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增项目组')" />
    <th:block th:include="include :: ztree-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-prjgroup-add">
        <input name="deptId" type="hidden" id="treeId"/>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">项目组名称：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="prjgroupName" id="prjgroupName" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">编号：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="code" id="code" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">项目组介绍：</label>
            <div class="col-sm-8">
                <input id="remark" name="remark" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">项目组负责人：</label>
            <div class="col-sm-8">
                <input id="leader" name="leader" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">选择部门：</label>
            <div class="col-sm-8">
                <div class="input-group">
                    <input name="deptName" onclick="selectDeptTree()" id="treeName" type="text" placeholder="请选择归属部门" class="form-control">
                    <span class="input-group-addon"><i class="fa fa-search"></i></span>
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: ztree-js" />
<script type="text/javascript">
    //鉴定重复
    $("#form-prjgroup-add").validate({
        rules:{
            onkeyup: false,
            roleName:{
                remote: {
                    url: ctx + "system/role/checkRoleNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "roleName" : function() {
                            return $.common.trim($("#roleName").val());
                        }
                    },
                    dataFilter: function(data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
            roleKey:{
                remote: {
                    url: ctx + "system/role/checkRoleKeyUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "roleKey" : function() {
                            return $.common.trim($("#roleKey").val());
                        }
                    },
                    dataFilter: function(data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
            roleSort:{
                digits:true
            },
        },
        messages: {
            "roleName": {
                remote: "角色名称已经存在"
            },
            "roleKey": {
                remote: "角色权限已经存在"
            }
        },
        focusCleanup: true
    });

    /* 项目组管理-新增-选择部门树 */
    function selectDeptTree() {
        var treeId = $("#treeId").val();

        var deptId = $.common.isEmpty(treeId) ? "1" : $("#treeId").val();
        var url = ctx + "system/dept/selectDeptTree/" + deptId;
        var options = {
            title: '选择部门',
            width: "380",
            url: url,
            callBack: doSubmit
        };
        $.modal.openOptions(options);
    }

    function doSubmit(index, layero){
        var tree = layero.find("iframe")[0].contentWindow.$._tree;
        var body = layer.getChildFrame('body', index);
        $("#treeId").val(body.find('#treeId').val());
        $("#treeName").val(body.find('#treeName').val());
        layer.close(index);
    }

    $('input').on('ifChanged', function(obj){
        var type = $(this).val();
        var checked = obj.currentTarget.checked;
        if (type == 1) {
            if (checked) {
                $._tree.expandAll(true);
            } else {
                $._tree.expandAll(false);
            }
        } else if (type == "2") {
            if (checked) {
                $._tree.checkAllNodes(true);
            } else {
                $._tree.checkAllNodes(false);
            }
        } else if (type == "3") {
            if (checked) {
                $._tree.setting.check.chkboxType = { "Y": "ps", "N": "ps" };
            } else {
                $._tree.setting.check.chkboxType = { "Y": "", "N": "" };
            }
        }
    })

    function submitHandler() {
        if ($.validate.form()) {
            add();
        }
    }

    function add() {
        var data = $("#form-prjgroup-add").serializeArray();

        $.ajax({
            cache : true,
            type : "POST",
            url : ctx + "system/prjgroup/add",
            data : data,
            async : false,
            error : function(request) {
                $.modal.alertError("系统错误");
            },
            success : function(data) {
                $.operate.successCallback(data);
            }
        });
    }
</script>
</body>
</html>
